﻿using Domain.Entity;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
	public class ERRepository : IERRepository
	{
		private readonly MyDbContext myDb;

		public ERRepository(MyDbContext myDb)
		{
			this.myDb = myDb;
		}

		public async Task<bool> AddER(ER e)
		{
			myDb.ER.Add(e);
			int zhi = await myDb.SaveChangesAsync();
			return zhi > 0 ? true : false;
		}

		public async Task<bool> DeleteER(int e)
		{
			var zhi = await myDb.ER.SingleOrDefaultAsync(a => a.ResId == e);
			if (zhi != null)
			{
				myDb.ER.Remove(zhi);
				await myDb.SaveChangesAsync();
				return true;
			}
			return false;
		}

		public List<ER> GetER()
		{
			List<ER> list = myDb.ER.ToList();
			return list;
		}

		public List<ER> GetERTiaoJ(string? humanName, string? hunmaMajorName, string? humanMajorKindName, DateTime? startDate, DateTime? endDate)
		{
			var query = myDb.ER.AsQueryable();

			// 添加对HumanName的过滤条件（如果humanName不为空）
			if (!string.IsNullOrEmpty(humanName))
			{
				query = query.Where(e => e.HumanName == humanName);
			}

			// 添加对HumanMajorKindId的过滤条件（如果humanMajorKindId不为空）
			if (!string.IsNullOrEmpty(hunmaMajorName))
			{
				query = query.Where(e => e.HunmaMajorName == hunmaMajorName);
			}

			// 添加对HumanMajorKindName的过滤条件（如果humanMajorKindName不为空）
			if (!string.IsNullOrEmpty(humanMajorKindName))
			{
				query = query.Where(e => e.HumanMajorKindName == humanMajorKindName);
			}
			// 添加对RegistTime的时间范围过滤条件（如果startDate和endDate都被设置了）
			if (startDate.HasValue && endDate.HasValue)
			{
				query = query.Where(e => e.RegistTime >= startDate.Value && e.RegistTime <= endDate.Value);
			}

			// 执行查询并返回结果
			var users = query.ToList();
			return users;
		}


		public List<ER> GetERName(string name)
		{
			var user = myDb.ER.Where(e => e.PassCheckStatus == name).ToList();
			return user;
		}

		public List<ER> GetERYouxiaoJianl(string? passPassComment, string? humanName, string? hunmaMajorName, string? humanMajorKindName, DateTime? startDate, DateTime? endDate)
		{
			var query = myDb.ER.AsQueryable();

			// 添加对PassPassComment的过滤条件（如果passPassComment不为空）
			if (!string.IsNullOrEmpty(passPassComment))
			{
				query = query.Where(e => e.PassCheckStatus == passPassComment);
			}
			// 添加对HumanName的过滤条件（如果humanName不为空）
			if (!string.IsNullOrEmpty(humanName))
			{
				query = query.Where(e => e.HumanName == humanName);
			}

			// 添加对HumanMajorKindId的过滤条件（如果humanMajorKindId不为空）
			if (!string.IsNullOrEmpty(hunmaMajorName))
			{
				query = query.Where(e => e.HunmaMajorName == hunmaMajorName);
			}

			// 添加对HumanMajorKindName的过滤条件（如果humanMajorKindName不为空）
			if (!string.IsNullOrEmpty(humanMajorKindName))
			{
				query = query.Where(e => e.HumanMajorKindName == humanMajorKindName);
			}

			// 添加对RegistTime的时间范围过滤条件（如果startDate和endDate都被设置了）
			if (startDate.HasValue && endDate.HasValue)
			{
				query = query.Where(e => e.RegistTime >= startDate.Value && e.RegistTime <= endDate.Value);
			}


			// 执行查询并返回结果
			var users = query.ToList();
			return users;
		}

		public async Task<bool> UpdateER(ER e)
		{

			var zhi = await myDb.ER.FindAsync(e.ResId);
			if (zhi == null)
			{
				return false;
			}
			zhi.PassCheckStatus = e.PassCheckStatus;
			zhi.PassCheckTime = e.PassCheckTime;
			zhi.PassChecker = e.PassChecker;

			myDb.ER.Update(zhi);
			int xg = await myDb.SaveChangesAsync();
			return xg > 0;
		}

		public List<ER> GetAllERId(int id)
		{
			var zhi = myDb.ER.Where(e => e.ResId == id).ToList();
			return zhi;
		}
		public async Task<bool> UpdateERTuijian(ER e)
		{

			var zhi = await myDb.ER.FindAsync(e.ResId);
			if (zhi == null)
			{
				return false;
			}

			zhi.Recomandation = e.Recomandation; //推荐意见
			zhi.Checker = e.Checker; //复核人
			zhi.CheckTime = e.CheckTime; //复核时间
			zhi.CheckStatus = e.CheckStatus; //复核状态

			myDb.ER.Update(zhi);
			int xg = await myDb.SaveChangesAsync();
			return xg > 0;
		}

		public async Task<bool> UpdateERMianshi(ER e)
		{
			var zhi = await myDb.ER.FindAsync(e.ResId);
			if (zhi == null)
			{
				return false;
			}

			zhi.InterviewStatus = e.InterviewStatus; //面试状态
			zhi.PassCheckStatus = e.PassCheckStatus; //通过的复核状态
			zhi.PassCheckTime = e.PassCheckTime; //通过复核时间
			zhi.PassChecker = e.PassChecker;     //通过复核人姓名

			myDb.ER.Update(zhi);

			int xg = await myDb.SaveChangesAsync();
			return xg > 0;
		}

		public async Task<bool> UpdateERLuyong(ER e)
		{
			var zhi = await myDb.ER.FindAsync(e.ResId);
			if (zhi == null)
			{
				return false;
			}

			zhi.PassCheckComment = e.PassCheckComment;  //录用审核
			zhi.PassPassComment = e.PassPassComment;  //录用审批

			myDb.ER.Update(zhi);

			int xg = await myDb.SaveChangesAsync();
			return xg > 0;
		}
	}
}
